const SelectPhotoModel = require("../../model/personal/select-photo/index");
const UpdateIntroModel = require("../../model/personal/update_intro/index");
const UpdateUserNameModel = require("../../model/personal/update-user-name/index");
const ReleaseRecordByUserIDModel = require("../../model/personal/release-record-by-userID/index");
const CollectRecordByUserIDModel = require("../../model/personal/collect-record-by-userID/index");
const FansModel = require("../../model/personal/fans/index");
const DeleteModel = require("../../model/personal/delete/index");

const userCenter = {
    // 查询头像
    select_photo: async (req, res) => {
        let newData = req.query
        await SelectPhotoModel(newData).then(data => {
            if (data.length > 0) {
                res.returnMsg(data, '查询成功', 200)
            } else {
                res.returnMsg(null, '系统错误，请联系管理员', 500)
            }
        })
    },
    // 修改个人简介
    update_intro: async (req, res) => {
        const newData = req.body
        await UpdateIntroModel(newData).then(data => {
            if (data.changedRows == 1) {
                res.returnMsg({list: data}, '修改成功', 200)
            } else {
                res.returnMsg(null, '系统错误，请联系管理员', 500)
            }
        })
    },
    // 修改账号名称
    update_user_name: async (req, res) => {
        const newData = req.body
        await UpdateUserNameModel(newData).then(data => {
            if (data.changedRows == 1) {
                res.returnMsg({list: data}, '修改成功', 200)
            } else {
                res.returnMsg(null, '系统错误，请联系管理员', 500)
            }
        })
    },
    // 查询用户的发布记录-根据userID查询
    release_record_by_userID: async (req, res) =>{
        const userID = req.query.userID
        await ReleaseRecordByUserIDModel(userID).then(data =>{
            // if(data.length){
                res.returnMsg({list: data}, '查询成功', 200)
            // }else{
            //     res.returnMsg(null, '系统错误或用户不存在', 500)
            // }
        })
    },
    // 查询用户的收藏记录-根据userID查询
    collect_record_by_userID: async (req, res) =>{
        const userID = req.query.userID
        await CollectRecordByUserIDModel(userID).then(data =>{
            console.log(data)
            // if(data){
                res.returnMsg({list: data}, '查询成功', 200)
            // }else{
            //     res.returnMsg(null, '系统错误或数据不存在', 500)
            // }
        })
    },
    // 查询用户的粉丝量
    fans: async (req, res) =>{
        const userID = req.query.userID
        await FansModel(userID).then(data =>{
            if(data.length){
                res.returnMsg({list: data}, '查询成功', 200)
            }else{
                res.returnMsg(null, '系统错误或用户不存在', 500)
            }
        })
    },
    // 删除用户发布的帖子
    delete: async (req, res) =>{
        const postID = req.body.postID
        console.log(postID)
        await DeleteModel(postID).then(data =>{
            if(data.affectedRows == 1){
                res.returnMsg({list: data}, '删除成功', 200)
            }else{
                res.returnMsg(null, '系统错误或数据不存在', 500)
            }
        })
    },
}
module.exports = userCenter;
